package com.dhl.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * web项目配置类
 * @title: CorsConfig
 * @Author danghailiang
 * @Date: 2022/9/23 19:57
 * @Version 1.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .maxAge(3600);
    }

    /**
     * 映射静态文件夹到服务器
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        StringBuffer dir = new StringBuffer(System.getProperty("user.dir"));
//        dir.append("\\src\\main\\resources\\static\\image\\userAvatar");
//        ClassPathResource classPathResource = new ClassPathResource("static");
//        try {
//            File file1 = classPathResource.getFile();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        registry.addResourceHandler("/image/userAvatar/**").addResourceLocations("file:"+ dir.toString());
        registry.addResourceHandler("/image/userAvatar/**").addResourceLocations("file:F:\\exerciseProject\\auth_base\\back\\src\\main\\resources\\static\\image\\userAvatar\\");
    }
}
